<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 一维码类
 * @package  module/base/common
 * @author   allen.qiang
 * @version 1.0
 */

quanli_require('framework/barcode/class/BCGFontFile.php');
quanli_require('framework/barcode/class/BCGColor.php');
quanli_require('framework/barcode/class/BCGDrawing.php');

class quanliBarcode {

    /**
     * 生成一维码
     * @param string $text 条码内容
     * @param array $extend_array 扩展数组
     * @return array
     */
    public function create( $text, $extend_array = array() ) {

        if (!isset($text) || empty($text)) {
            return array( 'code' => 400, 'mesage' => '参数错误' );
        }

        //条码文本字体大小
        if (isset($extend_array['text_size']) && !empty($extend_array['text_size'])) {
            $text_size = $extend_array['text_size'];
        } else {
            $text_size = 25;
        }

        //条码分辨率
        if (isset($extend_array['resolution']) && !empty($extend_array['resolution'])) {
            $resolution = $extend_array['resolution'];
        } else {
            $resolution = 4;
        }

        //条码高度
        if (isset($extend_array['thickness']) && !empty($extend_array['thickness'])) {
            $thickness = $extend_array['thickness'];
        } else {
            $thickness = 70;
        }

        //条码类型
        if (isset($extend_array['codebar']) && !empty($extend_array['codebar'])) {
            $codebar = $extend_array['codebar'];
        } else {
            $codebar = 'BCGcode39';
        }

        //加载不同类型的条码生成类
        quanli_require('framework/barcode/class/' . $codebar . '.barcode.php');

        //文本字体
        $font = new BCGFontFile(G_APPLICATION_BASEPATH . '/framework/barcode/font/Arial.ttf', $text_size);

        //一维码背景颜色和条码颜色
        $color_black = new BCGColor(0, 0, 0);
        $color_white = new BCGColor(255, 255, 255);

        //设置参数
        try {
            $code = new $codebar();
            $code->setScale($resolution);
            $code->setThickness($thickness);
            $code->setForegroundColor($color_black);
            $code->setBackgroundColor($color_white);
            $code->setFont($font);
            $code->parse($text);
        } catch(Exception $exception) {
            return array( 'code' => 404, 'mesage' => '一维码生成失败' );
        }

        //检查条码目录
        $dirname = G_IMAGE_SAVE_ROOT . 'barcode/';
        if (!file_exists($dirname)) {
            mkdir($dirname, 0777, true );
        }

        //生成条形码
        $fileName = $dirname . $text . '.png';
        $drawing = new BCGDrawing($fileName, $color_white);
        $drawing->setBarcode($code);
        $drawing->draw();

        //保存条形码
        $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

        return array( 'code' => 200, 'mesage' => 'ok' );

    }

    /**
     * 获得条形码图片链接
     * @param string $barcode 条形码
     * @param array $extend_array 扩展数组
     * @return array
     */
    public function getBarcodeUrl( $barcode, $extend_array = array() ) {

        $fileName =  G_IMAGE_SAVE_ROOT . 'barcode/' . $barcode . '.png';
        if (!file_exists($fileName)) {
            $result_array = $this->create( $barcode, $extend_array );
            if ($result_array['code'] != 200) {
                return $result_array;
            }
        }

        $barcode_url = G_IMAGE_URL . 'barcode/' . $barcode . '.png';

        return array( 'code' => 200, 'message' => 'ok', 'data' => array( 'barcode_url' => $barcode_url ) );

    }

}